﻿using Dapr.Actors;
using Dapr.Actors.Client;
using Microsoft.AspNetCore.Mvc;
using MyActor.Interfaces;
using MyActorService.Models;
using System.Diagnostics;

namespace MyActorService.Controllers
{
    public class ActorController : Controller
    {
        private readonly ILogger<ActorController> _logger;

        public ActorController(ILogger<ActorController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<ActionResult> CreateOrder(string id)
        {
            var _items = new List<OrderItem>() { new OrderItem() { Product = new Product { Id = id }, Quantity = 2 } };
            Order order = new Order() { Items = _items };
            try
            {
                string responseId = "";
                // 扣除该订单每个商品的库存        
                foreach (var orderItem in order.Items)
                {
                    //产品Id作为 Actor Id            
                    var actorId = new ActorId(orderItem.Product.Id);
                    //创建调用代理, 只需知道接口(IInventory)即可            
                    var proxy = ActorProxy.Create<IInventory>(actorId, "InventoryActor");
                    responseId = await proxy.SubtractAsync(orderItem,actorId);
                }
                return Ok(responseId);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message); 
            }
        }
        public async Task<ActionResult> Order(string id)
        { 
            var _items = new List<OrderItem>() { new OrderItem() { Product = new Product { Id = id }, Quantity = 2 } };
            Order order = new Order() { Items = _items };
            try
            {
                string responseId = "";
                // 扣除该订单每个商品的库存        
                foreach (var orderItem in order.Items)
                {
                    //产品Id作为 Actor Id            
                    var actorId = new ActorId(orderItem.Product.Id);
                    //创建调用代理, 只需知道接口(IInventory)即可            
                    var proxy = ActorProxy.Create<IInventory>(actorId, "InventoryActor");
                    responseId = await proxy.SubtractAsync(orderItem, actorId);
                }
                return Ok(responseId);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        public async Task<int> GetActorId()
        {
            //创建调用代理, 只需知道接口(IInventory)即可            
            var proxy = ActorProxy.Create<IMyActor>(new ActorId("1"), "MyActor");
            return await proxy.NextId();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}